// pages/authorizedLogin/authorizedLogin.js
const app = getApp();
var config = require('../../config.js');
var host = app.globalData.host;
var util = require('../../utils/util.js');
var WXBizDataCrypt = require('../../utils/cryptojs/RdWXBizDataCrypt.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:0
  },

  onGetUserInfo: function (e) {
  var that = this;
  wx.login({
    success: function(res) {
      if(res.code){
        that.getOpenId(res.code);
      }
    },
    
  })

    var appId = 'wx0521d13c2d02f9e3'
    
    // var encryptedData = e.detail.encryptedData;
    // var iv = e.detail.iv;
    // console.info(sessionKey);
    // var pc = new WXBizDataCrypt(appId, sessionKey);
    // var data = pc.decryptData(encryptedData, iv);
    // console.log('解密后 data: ', data)
    
  },
  getOpenId: function (code) {
    var _this = this
    wx.request({
      url: config.openIdUrl,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        "code": code,
      },
      success: function (result) {
        wx.setStorageSync("sessionKey", result.data.session_key);
        var sessionKey = wx.getStorageSync("sessionKey")
        if (sessionKey != "") {
          _this.setData({
            status: 1
          })
        }
      },
    })
  },
  getPhoneNumber: function (e) {
    var that = this;
    var sessionKey = wx.getStorageSync("sessionKey")
    var encryptedData = e.detail.encryptedData;
    var iv = e.detail.iv;
    var appId = 'wx0521d13c2d02f9e3'
    var pc = new WXBizDataCrypt(appId, sessionKey);
    var data = pc.decryptData(encryptedData, iv);
    wx.setStorageSync("phoneNumber", data.phoneNumber);
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      
      var userPhone = wx.getStorageSync("phoneNumber");
      var that = this;
      if (userPhone == "" || userPhone == null || userPhone == undefined) {
        app.tip("授权之后才可以登录", "none");
        return false;
      }
      console.info(userPhone)

      var userType = '1';
      wx.request({
        url: config.loginUrl,
        method: 'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data: {
          "userPhone": userPhone,
          "userType": userType
        },
        success: function (result) {
          console.info("登录");
          console.info(result);
          var data = result.data.object;
          if (data != null) {
            wx.setStorageSync("companyInfo", null);
            wx.setStorageSync("uid", 0);
            wx.setStorageSync("workerInfo", data);
            that.setData({
              uid: 0,
              modalName: null
            })

            wx.reLaunch({
              url: '/pages/index/index',
            })
            
          } else {
            if (result.data.code == '201') {
              app.tip("该账号是企业账号,不可以登录个人系统", "none");
            } else {
              wx.navigateTo({
                url: '../message/message',
              })
            }
          }
        },
        fail: function ({ errMsg }) {
          wx.hideLoading()
          app.tip(errMsg, 'none')
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})